/**
  ******************************************************************************
  * @file    mbmap.c
  * @brief   This file provides code for the configuration
  *          of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "modbus.h"
#include "gv.h"
#include "tskcfg.h"
#include <string.h>

#define tofloat(x)   (float)x

void mb_inputreg_rd_update(Modbus_MapIntputRegStru *ir)
{
  ir->rtc_year   = sys_datatime.year+2000;
  ir->rtc_month  = sys_datatime.month;
  ir->rtc_day    = sys_datatime.day;
  ir->rtc_hour   = sys_datatime.hour;
  ir->rtc_minute = sys_datatime.minute;
  ir->rtc_second = sys_datatime.second;
  
  ir->weight_factor_base = gv.hr.meas_pa.weight_factor_base;
  ir->zero_code_ave = gv.hr.meas_pa.zero_code_ave;
  ir->zero_code_uplimit = gv.hr.meas_pa.zero_code_uplimit;
  ir->len_circle_mode = gv.hr.meas_pa.len_circle_mode;
  ir->manu_zero_range = gv.hr.meas_pa.manu_zero_range;
//  ir->auto_zero_onoff = gv.hr.meas_pa.
  ir->auto_zero_range = gv.hr.meas_pa.auto_zero_range;

  ir->flowrate_lowlim_tph = tofloat(gv.hr.meas_pa.flowrate_lowlim_kgh)/1000.0;
  ir->wad_preset_enable = gv.hr.meas_pa.wad_preset_enable;
  ir->wad_preset_value = gv.hr.meas_pa.wad_preset_value;
  ir->sad_preset_enable = gv.hr.meas_pa.sad_preset_enable;
  ir->sad_preset_value = gv.hr.meas_pa.sad_preset_value;

  ir->belt_period_s = tofloat(gv.hr.meas_pa.belt_period_0d01s)/100.0;
  ir->belt_length_m = tofloat(gv.hr.meas_pa.belt_length_mm)/1000.0;
  ir->effect_length_mm = tofloat(gv.hr.meas_pa.effect_length_mm);
  ir->speed_wheel_pls = gv.hr.meas_pa.speed_wheel_pls;
  ir->speed_wheel_cir_mm = tofloat(gv.hr.meas_pa.speed_wheel_cir_0d1mm)/10.0;
  ir->measuring_range_tph = tofloat(gv.hr.meas_pa.measuring_range_tph);
  ir->cal_shiwu_wtacc = gv.hr.meas_pa.cal_shiwu_wtacc;
  
  ir->prod_year_capacity_t = tofloat(gv.hr.meas_pa.prod_year_capacity_t);
  ir->prod_month_capacity_t = tofloat(gv.hr.meas_pa.prod_month_capacity_t);
  ir->prod_day_capacity_t = tofloat(gv.hr.meas_pa.prod_day_capacity_t);
  ir->prod_hour_capacity_t = tofloat(gv.hr.meas_pa.prod_hour_capacity_t);

  ir->weight_code_ave = gv.ir.meas_ca.weight_code_ave;
  ir->speed_code_ave = gv.ir.meas_ca.speed_code_ave;
  ir->flowrate_tph = tofloat(gv.ir.meas_ca.flowrate_kgh)/1000.0;
  ir->flowrate_zn_tph = tofloat(gv.ir.meas_ca.flowrate_kgh)/1000.0;
  ir->belt_speed_mms = tofloat(gv.ir.meas_ca.belt_speed_mms);

  strncpy((char *)ir->ics_num, sys_version.sn, 16);
  _snprintf((char *)ir->wt_sensor_01_num, 16, "%llu", gv.ir.meas_ca.wt_sensor_info[0].sn);
  _snprintf((char *)ir->wt_sensor_02_num, 16, "%llu", gv.ir.meas_ca.wt_sensor_info[1].sn);
  _snprintf((char *)ir->wt_sensor_03_num, 16, "%llu", gv.ir.meas_ca.wt_sensor_info[2].sn);
  _snprintf((char *)ir->wt_sensor_04_num, 16, "%llu", gv.ir.meas_ca.wt_sensor_info[3].sn);
  _snprintf((char *)ir->speed_sensor_num, 16, "%llu", gv.ir.meas_ca.spd_sensor_info.sn);
  ir->wt_sensor_01_state = gv.ir.meas_ca.wt_sensor_info[0].state;
  ir->wt_sensor_02_state = gv.ir.meas_ca.wt_sensor_info[1].state;
  ir->wt_sensor_03_state = gv.ir.meas_ca.wt_sensor_info[2].state;
  ir->wt_sensor_04_state = gv.ir.meas_ca.wt_sensor_info[3].state;
  ir->speed_sensor_state = gv.ir.meas_ca.spd_sensor_info.state;
  ir->wt_sensor_01_code = gv.ir.meas_ca.wt_sensor_info[0].srccode;
  ir->wt_sensor_02_code = gv.ir.meas_ca.wt_sensor_info[1].srccode;
  ir->wt_sensor_03_code = gv.ir.meas_ca.wt_sensor_info[2].srccode;
  ir->wt_sensor_04_code = gv.ir.meas_ca.wt_sensor_info[3].srccode;
  ir->speed_sensor_code = gv.ir.meas_ca.spd_sensor_info.srccode;

  ir->total_acc_t = tofloat(gv.ir.meas_ca.total_acc_0d1g/10000)/1000.0;
  ir->year_acc_t = tofloat(gv.ir.meas_ca.year_acc_0d1g/10000)/1000.0;
  ir->month_acc_t = tofloat(gv.ir.meas_ca.month_acc_0d1g/10000)/1000.0;
  ir->day_acc_t = tofloat(gv.ir.meas_ca.day_acc_0d1g/10000)/1000.0;
  ir->hour_acc_t = tofloat(gv.ir.meas_ca.hour_acc_0d1g/10000)/1000.0;
  ir->class_acc_t = tofloat(gv.ir.meas_ca.class01_acc_0d1g/10000)/1000.0;
}

void mb_holdreg_rd_update(Modbus_MapHoldregStru *hr)
{
  hr->weight_factor_base = gv.hr.meas_pa.weight_factor_base;
  hr->zero_code_ave = gv.hr.meas_pa.zero_code_ave;
  hr->zero_code_uplimit = gv.hr.meas_pa.zero_code_uplimit;
  hr->len_circle_mode = gv.hr.meas_pa.len_circle_mode;
  hr->manu_zero_range = gv.hr.meas_pa.manu_zero_range;
//  hr->auto_zero_onoff = gv.hr.meas_pa.
  hr->auto_zero_range = gv.hr.meas_pa.auto_zero_range;

  hr->flowrate_lowlim_tph = tofloat(gv.hr.meas_pa.flowrate_lowlim_kgh)/1000.0;
  hr->wad_preset_enable = gv.hr.meas_pa.wad_preset_enable;
  hr->wad_preset_value = gv.hr.meas_pa.wad_preset_value;
  hr->sad_preset_enable = gv.hr.meas_pa.sad_preset_enable;
  hr->sad_preset_value = gv.hr.meas_pa.sad_preset_value;

  hr->belt_period_s = tofloat(gv.hr.meas_pa.belt_period_0d01s)/100.0;
  hr->belt_length_m = tofloat(gv.hr.meas_pa.belt_length_mm)/1000.0;
  hr->effect_length_mm = tofloat(gv.hr.meas_pa.effect_length_mm);
  hr->speed_wheel_pls = gv.hr.meas_pa.speed_wheel_pls;
  hr->speed_wheel_cir_mm = tofloat(gv.hr.meas_pa.speed_wheel_cir_0d1mm)/10.0;
  hr->measuring_range_tph = tofloat(gv.hr.meas_pa.measuring_range_tph);
  hr->cal_shiwu_wtacc = gv.hr.meas_pa.cal_shiwu_wtacc;
  
  hr->prod_year_capacity_t = tofloat(gv.hr.meas_pa.prod_year_capacity_t);
  hr->prod_month_capacity_t = tofloat(gv.hr.meas_pa.prod_month_capacity_t);
  hr->prod_day_capacity_t = tofloat(gv.hr.meas_pa.prod_day_capacity_t);
  hr->prod_hour_capacity_t = tofloat(gv.hr.meas_pa.prod_hour_capacity_t);
}
